<?php
/*
	
*/
defined('BY_JYA') or exit('Access Denied');
global $_RQ,$_AR;
$basic=$this->plugin_setting('basic');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return['diy']=$this->plugin_setting('diy');
$return['basic']=$this->plugin_setting('basic');
$return['banner']=$basic['image'];
if($_RQ['op']=='list'){
	//超时未支付订单取消
	$list=pdo_getall('jy_yuyue_order',array('uniacid'=>$_SESSION['uniacid'],'status'=>'0'));
	if($list){
		$timeout=$return['basic']['timeout']?$return['basic']['timeout']:5;
		foreach($list as $row){
			if(time()-$row['createtime']>$timeout*60){
				pdo_update('jy_yuyue_order',array('status'=>3),array('id'=>$row['id']));
			}
		}
	}
	$serve_list=pdo_getall('jy_yuyue_serve',array('uniacid'=>$_SESSION['uniacid'],'status'=>'1'),'','','paixu asc');
	foreach($serve_list as &$item){
		//已支付，已确认，已完成订单数
		$num1=pdo_count('jy_yuyue_order',array('uniacid'=>$_SESSION['uniacid'],'serve_id'=>$item['id'],'status'=>'1'));
		$num2=pdo_count('jy_yuyue_order',array('uniacid'=>$_SESSION['uniacid'],'serve_id'=>$item['id'],'status'=>'2'));
		$num3=pdo_count('jy_yuyue_order',array('uniacid'=>$_SESSION['uniacid'],'serve_id'=>$item['id'],'status'=>'4'));
		$item['num']=$num1+$num2+$num3;
	}
	$return['post']=array();
	$return['serve_list']=$serve_list;
	$sharelink='jy_park_yuyue/serve//fromuser='.$_SESSION['uid'];
	$return['sharedata']=array(
		'title'=>$return['pluginset']['share_title']?:'项目列表',
		'desc'=>$return['pluginset']['share_desc']?:'在线预约服务，一个属于自己的在线预约服务！',
		'link'=>custom_url($sharelink,true),
		'imgUrl'=>tomedia($return['pluginset']['share_image'])
	);
	set_html('title','项目列表');
	exi($return);
}
if($_RQ['op']=='sever_detail'){
	//exi($_SESSION['uid']+1,'error');
	if(!$_RQ['id']){
		exi('没有找到该项目，请重新选择！','error','jy_yuyue/sever/list');
	}
	//查询项目具体信息
	$serve=pdo_get('jy_yuyue_serve',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']));
	$return['banner']=$serve['hdps']?json_decode($serve['hdps'],true):$return['banner'];
	$package=$serve['package'];
	//查询本项目可服务员工(未锁定，状态为可服务)
	$worker_list=pdo_getall('jy_yuyue_worker',array('uniacid'=>$_SESSION['uniacid'],'islock'=>'0','status'=>'1'));
	foreach($worker_list as $k=>$item){
		$service=explode(";",$item['service']);
		if(!in_array($_RQ['id'],$service)){
			unset($worker_list[$k]);
		}
	}
	$worker_list=array_values($worker_list);
	if($serve['worker_id']){
		$worker_list[]=pdo_get('jy_yuyue_worker',array('id'=>$serve['worker_id'],'uniacid'=>$_SESSION['uniacid'],'islock'=>'0','status'=>'1'));
	}
	//查询自定义字段
	/*$fileds=pdo_getall('jy_yuyue_fileds',array('uniacid'=>$_SESSION['uniacid'],'serve_id'=>$_RQ['id']));
	foreach($fileds as &$item){
		$item['value']=explode(';',$item['value']);
	}*/
	//查询本项目的访问人数及添加访问人数
	$num=pdo_getcolumn('jy_yuyue_serve',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']),'num');
	$serve['num']=$num+1;
	pdo_update('jy_yuyue_serve',array('num'=>$serve['num']),array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']));
	//已支付，已确认，已完成订单数
	$serve['ordernum']=pdo_getsum('jy_yuyue_order',array('uniacid'=>$_SESSION['uniacid'],'serve_id'=>$_RQ['id'],'status'=>array(1,2,4)),'total');
	//显示评论
	$return['commentorderid']=pdo_getcolumn('jy_yuyue_order',array('uniacid'=>$_SESSION['uniacid'],'serve_id'=>$_RQ['id'],'status'=>array(2,4)),'id');
	$serve_time=json_decode($serve['serve_time'],true);
	//设置日期
	$data=array();
	$weekarray=array("日","一","二","三","四","五","六"); //先定义一个数组
	if($serve['time_type']){
		$serve['work_timedays']=$serve['work_timedays']?json_decode($serve['work_timedays'],true):array();	
		foreach($serve['work_timedays'] as $sv){
				$time=strtotime($sv['day']);
				$data[]=array('riqi'=>date("m-d",$time),'xinq'=>"星期".$weekarray[date("w",strtotime(date("Y-m-d",$time)))],'isshow'=>1);
		}
		$iswork=1;
	}else{		
		$days=$basic['days']>0?$basic['days']:7;
		for($i=0;$i<$days;$i++){
			$data[]=array('riqi'=>date("m-d",strtotime("+".$i." day")),'xinq'=>"星期".$weekarray[date("w",strtotime(date("Y-m-d",strtotime("+".$i." day"))))],'isshow'=>($serve['advancedays']>0&&$i<$serve['advancedays'])?-1:1);
		}
		$serve['advancedays']=$serve['advancedays']?:0;
		$weekid=date("w",strtotime(date("Y-m-d",strtotime("+".$serve['advancedays']." day"))));
		$weekid=$weekid?:7;
		$iswork=strpos($serve['work_time'],';'.$weekid.';');
	}
	$this->session('serve_id',$_RQ['id']);
	$return['serve']=$serve;
	$return['serve_id']=$serve['id'];
	$return['package']=json_decode($package);
	$return['worker_list']=$worker_list;
	$return['province']=file_get_contents('core/resource/area.txt');
	$return['province']=unserialize($return['province']);
	$return['serve_time']=$serve_time;
	$return['data']=$data;
	$return['post']=array('timeid'=>0);
	$return['post']['dayid']=$serve['time_type']?0:$serve['advancedays'];
	$return['post']['fileds']=$fileds;
	set_html('title',$serve['title']);
	exi($return);
}
if($_RQ['op']=='check_buy'){
	exi('','','jy_yuyue/book//serve_id='.$_RQ['serve_id'].'&worker_id='.$_RQ['worker_id'].'&packageid='.$_RQ['packageid'].'&timeid='.$_RQ['timeid'].'&dayid='.$_RQ['dayid']);
}
if($_RQ['op']=='notice'){
	$postdata=get_postdata();
	$vip=$this->plugin_setting('vip');
	$isvip=pdo_getcolumn('jy_yuyue_user',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_SESSION['uid']),'isvip');
	if($vip['openvip']==1 && $isvip==0){
		exi('请先激活','error','jy_yuyue/buyvip/');
	}
	$serve_id = $this->session('serve_id');
	$data=$this->days($serve_id,$basic['days']);		
	if($postdata){
		if(!$postdata['packageid']){
			exi('请选择服务套餐','error');
		}
		if($basic['isworker']!=1 && $postdata['worker_id']==''){
			exi('请选择服务人员','error');
		}
	//	exi($postdata['timeid'],'error');
		if(!$postdata['timeid']){
			exi('请选择服务时间','error');
		}
		if(!$postdata['username']){
			exi('请输入联系人姓名','error');
		}
		if(!$postdata['mobile']){
			exi('请输入联系人手机号码','error');
		}  
		if(!preg_match("/^1[345678]{1}\d{9}$/",$postdata['mobile'])){  
	 
			exi('请输入正确的手机号码','error');
		} 
		//print_r($postdata['fileds']);exit;
		if($postdata['fileds']){
			foreach ($postdata['fileds'] as $item) {
				if($item['ismust']==1 && empty($item['data'])){
					exi('请完善'.$item['title'].'内容后再提交','error');
				}
			}
		}
		//判断待支付订单及判断是否超过可预订次数
		
		$serve=pdo_get('jy_yuyue_serve',array('id'=>$serve_id));
		if($serve['xianzhi']==1){
			//某天
			$todaysum=pdo_count('jy_yuyue_order',array('uniacid'=>$_SESSION['uniacid'],'user_id'=>$_SESSION['uid'],'serve_id'=>$serve_id,'status >'=>'0','day'=>$data[$postdata['dayid']]['ymd']));
			$cancelsum=pdo_count('jy_yuyue_order',array('uniacid'=>$_SESSION['uniacid'],'user_id'=>$_SESSION['uid'],'serve_id'=>$serve_id,'status'=>'3','day'=>$data[$postdata['dayid']]['ymd']));
			$todaysum=$todaysum-$cancelsum;
			if($todaysum>=$serve['daytimes']){
				exi('你可预约该项目该日次数已达上限,请选择其他日期','error');
			}
			//所有
			$allsum=pdo_count('jy_yuyue_order',array('uniacid'=>$_SESSION['uniacid'],'user_id'=>$_SESSION['uid'],'serve_id'=>$serve_id,'status >'=>'0'));
			$cancelall=pdo_count('jy_yuyue_order',array('uniacid'=>$_SESSION['uniacid'],'user_id'=>$_SESSION['uid'],'serve_id'=>$serve_id,'status'=>'3'));
			$allsum=$allsum-$cancelall;
			if($allsum>=$serve['alltimes']){
				exi('你可预约总次数已达上限,请选择其他项目','error');
			}
		
		}
		$package=pdo_getcolumn('jy_yuyue_serve',array('uniacid'=>$_SESSION['uniacid'],'id'=>$serve_id),'package');
		$package=json_decode($package,true);
		if($postdata['packageid']){
			$packageinfo=$package[$postdata['packageid']-1];
			$price = $packageinfo['package_price'];
		}else{
			$price = pdo_getcolumn('jy_yuyue_serve',array('uniacid'=>$_SESSION['uniacid'],'id'=>$serve_id),'price');
		}
		$insert=array(
			'uniacid'=>$_SESSION['uniacid'],
			'user_id'=>$_SESSION['uid'],
			'worker_id'=>$postdata['worker_id'],
			'serve_id'=>$this->session('serve_id'),
			'package'=>ijson_encode($package[$postdata['packageid']-1]),
			'money'=>$price,
			'timeid'=>$postdata['timeid']-1,
			'day'=>$data[$postdata['dayid']]['ymd'],
			'address'=>$postdata['address'],
			'username'=>$postdata['username'],
			'mobile'=>$postdata['mobile'],
			'ordersn'=>get_ordersn('jy_yuyue_order','ordersn'),
			'remark'=>$postdata['remark'],
			'fileds'=>ijson_encode($postdata['fileds']),
			'createtime'=>TIMESTAMP,
		);
		pdo_insert('jy_yuyue_order',$insert);
		$id=pdo_insertid();
	}
	if($_SESSION['os'] == 'wxapp') {
		$return['orderid']=$id;
		exi($return);
	}else{
		exi('','','jy_yuyue/book/main/id='.$id);
	}
}
if($_RQ['op']=='days'){	
	if($_RQ['id']===''){
		exi('请选择项目','error');
	}
	$data=$this->days($_RQ['id'],$basic['days']);
	$return['data']=$data;
	$return['success']=1;
	exi($return);
	
}	
if($_RQ['op']=='serve_time'){
	if($_RQ['id']===''){
		exi('请选择项目','error');
	}
	$dayid=intval($_RQ['day']);
	if(!isset($_RQ['day'])){
		exi('请重新选择日期','error');
	}
	$data=array();
	//$data['0']=date("Y-m-d",time("day")); 
	//$data['1']=date("Y-m-d",strtotime("+1 day")); 
	//$data['2']=date("Y-m-d",strtotime("+2 day")); 
	//$data['3']=date("Y-m-d",strtotime("+3 day")); 
	//$data['4']=date("Y-m-d",strtotime("+4 day")); 
	//$data['5']=date("Y-m-d",strtotime("+5 day")); 
	//$data['6']=date("Y-m-d",strtotime("+6 day"));
	$data=$this->days($_RQ['id'],$basic['days']);
	$return['data']=$data;
	$post=get_postdata();
	$serve=pdo_get('jy_yuyue_serve',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']));
	$work_time=$serve['work_time'];
	if($serve['time_type']){
		$iswork=1;
	}else{
		$weekid=date("w",strtotime(date("Y-m-d",strtotime("+".$dayid." day"))));
		$weekid=$weekid?:7;
		if(strpos($work_time,';'.$weekid.';')!== false){
			$iswork=1;
		}
	}
	$serve_time=pdo_getcolumn('jy_yuyue_serve',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']),'serve_time');
	$serve_time=json_decode($serve_time,true);
	//获取当前时间
	$time=time();
	if($serve_time){
		foreach($serve_time as $k=>&$item){
			//待支付 已支付，已确认，已完成订单数
			$item['dayid']=$dayid;
			$item['day']=$data[$dayid]['ymd'];
			$num=pdo_getsum('jy_yuyue_order',array('timeid'=>$k,'uniacid'=>$_SESSION['uniacid'],'day'=>$item['day'],'serve_id'=>$_RQ['id'],'status'=>array(1,2,4)),'total');
			if($item['yuyue']>$num){
				$item['yuyue']=$item['yuyue']-$num;
			}else{
				$item['yuyue']=0;
			}
			//时间转为时间戳
			$item['shijian']=strtotime($data[$dayid]['ymd'].$item['times']['start']);
			if($item['shijian']<time() || !$iswork){
				$item['show']=0;
			}else{
				$item['show']=1;
			}
		
		}
	}
	$return['serve_time']=$serve_time;
	$return['success']=1;
	$return['weekid']=$weekid;
	$return['dayid']=$dayid;
	exi($return);
}
if($_RQ['op']=='package'){
	if(!$_RQ['id']){
		exi('请重新选择项目','error');
	}
	$package=pdo_getcolumn('jy_yuyue_serve',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']),'package');
	$package=json_decode($package,true);
	//查询自定义字段
	$fileds=pdo_getall('jy_yuyue_fileds',array('uniacid'=>$_SESSION['uniacid'],'serve_id'=>$_RQ['id']));
	foreach($fileds as &$item){
		$item['value']=explode(';',$item['value']);
	}
	$return['post']=array();
	$return['post']['fileds']=$fileds;
	$return['package']=$package;
	$return['success']=1;
	exi($return);
}
